package com.tucanstubs.android_wheresmystuff.activities;



import java.text.ParseException;

import com.tucanstubs.android_wheresmystuff.R;
import com.tucanstubs.android_wheresmystuff.login.Login;
import com.tucanstubs.android_wheresmystuff.login.LoginException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Activity for making a new account. The user fills in the relevant information and the account is 
 * created. 
 * @author team 37
 * @version 2.0 4/9/2013
 *
 */
public class SignUpScreenActivity extends Activity{
	
	private EditText userName, password, passwordCheck, firstName, lastName, eMail;
	
	/**
	 * Creates the activity and initializes all GUI components. 
	 */
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sign_up_screen);
		userName = (EditText) findViewById(R.id.Enter_User_Name);
		password = (EditText) findViewById(R.id.Enter_Password);
		passwordCheck = (EditText) findViewById(R.id.Re_Enter_Password_2);
		firstName = (EditText) findViewById(R.id.Enter_First_Name);
		lastName = (EditText) findViewById(R.id.Enter_Last_Name);
		eMail = (EditText) findViewById(R.id.Enter_Email);
	}
	

	/**
	 * Method which is launched when "sign up" is pressed, creating a new account is attempted.
	 *  If the user name is not already taken and the user entered all the proper info, 
	 *  then the account is created. 
	 * @param view The button which was pressed
	 */
	public void onClick(View view) {
		
		switch(view.getId()){
		case R.id.Sign_Up_Button:
			
			boolean isAdded = false;	
			Log.i(Tags.SIGNUPSCREEN, "Sign up button pressed");
			Toast toast;
			//try making a new account
			try {
				
				Log.i(Tags.SIGNUPSCREEN, userName.getText().toString());
				isAdded = Login.add(this,userName.getText().toString(), firstName.getText().toString(), lastName.getText().toString(), 
						password.getText().toString(), eMail.getText().toString(), passwordCheck.getText().toString());
			} catch (NullPointerException e) {
				
				Log.i(Tags.SIGNUPSCREEN, "Your Toast should be showing.");
				toast = Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} catch (LoginException e) {
				
				Log.i(Tags.SIGNUPSCREEN, "Exception Toast");
				toast = Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} catch (ParseException e) {
				
				e.printStackTrace();
			}
			
			if (isAdded) {
			
				Log.i(Tags.SIGNUPSCREEN, "Your File should have completed.");
				finish();
			}
		}
		
	}
}
